package dmwr.web;

import dmwr.Dmwr;
import dmwr.bean.Pool;
import dmwr.service.Service.Duplicate;

public class PoolAdmin {

	private static final int PAGE_SIZE = 15;

	public void list() {
		String page = Http.request().getParameter("page");
		int pageNum = page != null && page.length() > 0 ? Integer
				.parseInt(page) : 1;
		Http.request().setAttribute("page",
				Dmwr.service().listPools(pageNum, PAGE_SIZE));
		Http.forward("/admin/pool/list.jsp");
	}

	public void add() {
		Http.forward("/admin/pool/form.jsp");
	}

	public void edit() {
		Http.request().setAttribute(
				"pool",
				Dmwr.service().getPool(
						Long.parseLong(Http.request().getParameter("id"))));
		Http.forward("/admin/pool/form.jsp");
	}

	public void save() {
		Long id = Http.request().getParameter("id") != null ? Long
				.parseLong(Http.request().getParameter("id")) : null;
		Pool pool = new Pool(id, Http.request().getParameter("title"), null);
		try {
			Dmwr.service().savePool(pool);
			Http.redirect("/admin/pool");
		} catch (Duplicate e) {
			Http.redirect("/admin/pool?edit=&id="
					+ ((Pool) e.getExisting()).getId());
		}
	}

	public void delete() {
		Dmwr.service().deletePool(
				Long.parseLong(Http.request().getParameter("id")));
		Http.redirect("/admin/pool");
	}
}